package ch.ethz.smseth;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;

public class ContactUtilEclair extends ContactUtil
{
	public ContactUtilEclair(Context ctx)
	{
		super(ctx);
	}

	@Override
	public Contact fetchContactByName(String name)
	{
		StringBuilder buffer = new StringBuilder();
		buffer.append(ContactsContract.Data.DISPLAY_NAME);
		buffer.append(" = '").append(ContactUtil.escape(name)).append("'");
		buffer.append(" AND ").append(ContactsContract.Data.MIMETYPE);
		buffer.append(" = '").append(Phone.CONTENT_ITEM_TYPE).append("'");
		Cursor c = _ctx.getContentResolver().query(ContactsContract.Data.CONTENT_URI, PEOPLE_PROJECTION, buffer.toString(), null, null);
		if (c.moveToFirst())
		{
			Contact contact = cursorToContact(c);
			c.close();
			return contact;
		}
		else
		{
			c.close();
			return null;
		}
	}

	@Override
	public List<Contact> fetchContactsByName(String name)
	{
		StringBuilder buffer = new StringBuilder();
		buffer.append(ContactsContract.Data.DISPLAY_NAME);
		buffer.append(" = '").append(ContactUtil.escape(name)).append("'");
		buffer.append(" AND ").append(ContactsContract.Data.MIMETYPE);
		buffer.append(" = '").append(Phone.CONTENT_ITEM_TYPE).append("'");
		Cursor c = _ctx.getContentResolver().query(ContactsContract.Data.CONTENT_URI, PEOPLE_PROJECTION, buffer.toString(), null, null);
		List<Contact> result = new ArrayList<Contact>();
		
		if (c.moveToFirst())
		{
			do
			{
				result.add(cursorToContact(c));
			} while (c.moveToNext());
		}
		c.close();
		return result;
	}
	
	@Override
	public Contact fetchContactByNumber(String number)
	{
		StringBuilder buffer = new StringBuilder();
		buffer.append(Phone.NUMBER);
		buffer.append(" = '").append(ContactUtil.escape(number)).append("'");
		buffer.append(" AND ").append(ContactsContract.Data.MIMETYPE);
		buffer.append(" = '").append(Phone.CONTENT_ITEM_TYPE).append("'");
		Cursor c = _ctx.getContentResolver().query(ContactsContract.Data.CONTENT_URI, PEOPLE_PROJECTION, buffer.toString(), null, null);
		if (c.moveToFirst())
		{
			Contact contact = cursorToContact(c);
			c.close();
			return contact;
		}
		else
		{
			c.close();
			return null;
		}
	}

	@Override
	public List<Contact> fetchContacts(String constraint)
	{
		StringBuilder buffer = null;
		if (constraint != null)
		{
			buffer = new StringBuilder();
			buffer.append("(").append(ContactsContract.Data.DISPLAY_NAME);
			buffer.append(" LIKE '" + ContactUtil.escape(constraint) + "%' OR ");
			buffer.append(ContactsContract.Data.DISPLAY_NAME);
			buffer.append(" LIKE '% " + ContactUtil.escape(constraint) + "%')");
			buffer.append(" AND ").append(ContactsContract.Data.MIMETYPE);
			buffer.append(" = '").append(Phone.CONTENT_ITEM_TYPE).append("'");
		}

		Cursor c =  _ctx.getContentResolver().query(ContactsContract.Data.CONTENT_URI, PEOPLE_PROJECTION, buffer == null ? null : buffer.toString(), null, ContactsContract.Data.DISPLAY_NAME + " ASC");
		List<Contact> result = new ArrayList<Contact>();
		
		if (c.moveToFirst())
		{
			do
			{
				result.add(cursorToContact(c));
			} while (c.moveToNext());
		}
		c.close();
		return result;
	}

	private Contact cursorToContact(Cursor c)
	{
		Contact contact = new Contact();
		contact.name = c.getString(1);
		contact.number = c.getString(2);
		contact.label = _ctx.getResources().getString(Phone.getTypeLabelResource(c.getInt(3)));
		return contact;
	}
	
	private static final String[] PEOPLE_PROJECTION = new String[] { ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE };
}
